package com.hdoit.apollo.logical.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.CustomerRecord;
import com.hdoit.apollo.logical.shop.service.ICustomerRecordService;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年05月11日 11:04:41
 * @comment
 */
@Component
public class CustomerRecordServiceImpl extends SuperService<CustomerRecord> implements ICustomerRecordService{
	
	@Autowired
	private ICustomerService customerService;
	
	@Override
	public boolean saveCustomerRecord(String customerId, Double receivable, Double receipt, String type,
			String operateId, String shopId, String createUserId, String createUserName) {
		
		if(receivable==null) {
			receivable=0.0;
		}
		if(receipt==null) {
			receipt=0.0;
		}
		CustomerRecord customerRecord = new CustomerRecord();
		customerRecord.setModelStatus(ModelStatus.add);
		customerRecord.setRecordId(CommonUtils.getKeyId());
		customerRecord.setCustomerId(customerId);
		customerRecord.setReceivable(receivable);
		customerRecord.setReceipt(receipt);
		customerRecord.setType(type);
		customerRecord.setOperateId(operateId);
		customerRecord.setShopId(shopId);
		customerRecord.setCreateDate(DateUtils.getCurrent());
		customerRecord.setCreateUserId(createUserId);
		customerRecord.setCreateUserName(createUserName);
		customerRecord.setDeleteMark(YesOrNoInteger.no);
		
		Customer customer=customerService.queryModel(customerId);
		if(customer.getCustomerDebt()==null) {
			customer.setCustomerDebt(0.0);
		}
		customer.setModelStatus(ModelStatus.update);
		switch(type)
    	{
    	case "openBill":
    		customer.setCustomerDebt(customer.getCustomerDebt()+receivable);
    	  break;
    	case "modify":
    		customer.setCustomerDebt(customer.getCustomerDebt()+receivable);
    	  break;
    	case "close":
    		customer.setCustomerDebt(customer.getCustomerDebt()-receivable);
    	  break;
	    case "returnCargo":
	    	customer.setCustomerDebt(customer.getCustomerDebt()-receivable);
		  break;
	    case "returnOfCargo":
	    	customer.setCustomerDebt(customer.getCustomerDebt()-receivable);
		  break;
    	case "returnOfCargoModify":
    		customer.setCustomerDebt(customer.getCustomerDebt()-receivable);
    	  break;
	    case "pay":
	    	customer.setCustomerDebt(customer.getCustomerDebt()-receipt);
		  break;
	    case "refund":
	    	customer.setCustomerDebt(customer.getCustomerDebt()+receipt);
		  break;
		}
		System.err.println(customer.getCustomerDebt());
		customerRecord.setAccumulativeDebt(customer.getCustomerDebt());
		
		if(super.applyChange(customerRecord)) {
			if(!"pay".equals(type)&&!"refund".equals(type)) {
				return customerService.applyChange(customer);
			}
			return true;
		}
		return false;
	}

}